
/* File name	: input.c
 * Project 		: BKIT MCR
 * Version 		: 1.0
 * Creation Date: 07/07/2010
 * Tabsize		: 4
 * Author  		: BKIT4U                   
 * Copyright	: (c) 2010 BKIT HARDWARE CLUB - www.bkit4u.com
 * License		: Free.
 * Comments		: 
 * 
 */

/*====================================*/
/*  Include                           */
/*====================================*/
#include <avr/io.h>
#include "input.h"

/*====================================*/
/*	Global Variable Declaration       */
/*====================================*/
unsigned char key1, key2, key0, key_input;
unsigned char dipsw1, dipsw2, dipsw0, dipsw_input;
unsigned char extkey1, extkey2, extkey0, extkey_input;
unsigned char gatesensor1, gatesensor2, gatesensor0, gatesensor_input;

/*====================================*/
/*  Function name: input_init         */
/*  Parameter: null                   */
/*  Description:1. Configure hardware */
/*               for input function   */
/*              2. Dipswitch, push    */
/*                 button             */
/*====================================*/
void input_init(){
	PORTC = 0xff;			// dipswitch 4 and 4 push button on MCU Circuit
	DDRC  = 0x00;
	PORTE |= _BV(PE2);		// push button on "capacity" circuit
	DDRE  &= ~_BV(PE2);
	PORTE |= _BV(PE4);		// sensor to recognize gate
	DDRE  &= ~_BV(PE4);

	key1 = key2 = key0 = key_input = 0xf0;
	dipsw1 = dipsw2 = dipsw0 = dipsw_input = 0x00;
	extkey1 = extkey2 = extkey0 = extkey_input = 0;
}

/*====================================*/
/*  Function name: update_input       */
/*  Parameter: null                   */
/*  Description:update input value    */
/*             from dipswitch and push*/
/*             button                 */
/*====================================*/
void update_input(){
	unsigned char _temp;

	key2 = key1;
	key1 = key0;
	dipsw2  = dipsw1;
	dipsw1  = dipsw0;

	_temp = PINC;
	dipsw0  = _temp & SW_MASK;
	key0 = _temp & KEY_MASK;

	if((dipsw0==dipsw1)&&(dipsw1==dipsw2))
		dipsw_input = dipsw0;

  	if ((key0 == key1) && (key1 == key2)){
		key_input = key0;
	}

	extkey2 = extkey1;
	extkey1 = extkey0;
	if(PINE & _BV(PE2)) extkey0 = 0;
	else extkey0 = 1;
	if((extkey0==extkey1)&&(extkey1==extkey2))
		extkey_input = extkey0;

	gatesensor2 = gatesensor1;
	gatesensor1 = gatesensor0;
	if(PINE & _BV(PE4)) gatesensor0 = 0;
	else gatesensor0 = 1;
	if((gatesensor0==gatesensor1)&&(gatesensor1==gatesensor2))
		gatesensor_input = gatesensor0;
}

/*====================================*/
/*  Function name: get_key            */
/*  Parameter: _key_id                */
/*  Return value: unsigned char       */
/*  Description: get state of push    */
/*         button on MCU circuit      */
/*		   _key_id is KEY0, KEY1, KEY2*/
/*			or KEY3					  */
/*====================================*/
unsigned char get_key(unsigned char _key_id){
	if(key_input & _BV(_key_id))
		return 0;
	return 1;
}

/*====================================*/
/*  Function name: get_dipsw          */
/*  Parameter: null                   */
/*  Return value: unsigned char       */
/*  Description: get state of         */
/*                          dipswitch */
/*====================================*/
unsigned char get_dipsw(){
	return dipsw_input;
}

/*====================================*/
/*  Function name: get_extkey         */
/*  Parameter: null                   */
/*  Return value: unsigned char       */
/*  Description:get state of push     */
/*         button on capacity circuit */
/*====================================*/
unsigned char get_extkey(){
	return extkey_input;
}

/*====================================*/
/*  Function name: get_gatesensor     */
/*  Parameter: null                   */
/*  Return value: unsigned char       */
/*  Description: get state of  gate   */
/*               sensor               */
/*====================================*/
unsigned char get_gatesensor(){
	
	return gatesensor_input;
}


/****************END FILE**************/

